<?php

namespace App\Store\Models\Financial\Statements;

use App\Common\Services\Common;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\PaytypeList;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Rate extends Model
{
    protected $table = 'site_financial_statements_rate';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表
     */
    public static function getList(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['storeinfo','paytypeinfo'])->where(['cp_id' => $cp_id, 'is_del' => 1]);

        //目标门店
        $store_id = $request['store_id'] ?? 0;
        if ($store_id > 0) {
            $query->where('store_id', $store_id);
        }

        //订单状态
        $type = $request['type'] ?? 0;
        if ($type > 0) {
            $query->where('type', $type);
        }

        //费率类型
        $val_type = $request['val_type'] ?? 0;
        if ($val_type > 0) {
            $query->where('val_type', $val_type);
        }

        $query->orderBy('create_at', 'desc');

        $isgetall = $request['isgetall'] ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function storeinfo(){
        return $this->hasOne(SiteStore::class, 'id', 'store_id');
    }

    public function paytypeinfo(){
        return $this->hasOne(PaytypeList::class, 'id', 'pay_type_id');
    }
}

